import { defineStore } from "pinia";
import discussOjStore from "./discussion/index";

const useOjStore = defineStore("oj", {
  state: () => {
    return {
      discuss: discussOjStore(),
      systemParams: {
        screenWidth: 0,
        screenHeight: 0,
      },
      userData: {
        id: null,
        username: "",
        nickname: null,
        school: null,
        college: null,
        major: null,
        number: null,
        realName: null,
        gender: null,
        github: null,
        blog: null,
        email: null,
        avatar: null,
        signature: null,
        authLevel: null,
        roleList: [],
      },
      isLogin: false,
      raceStatus: {
        state: 0, //1进行中 其他未进行中
        format: null,
      },
      premission: [],
    };
  },
  getters: {
    getWidth() {
      return this.systemParams.screenWidth + "px";
    },
    getCardHeight() {
      return this.systemParams.screenHeight * 0.5 + "px";
    },
    getCardWidth() {
      return this.systemParams.screenWidth * 0.4 + "px";
    },
    getPermission(value) {
      return this.premission;
    },
    getUserAvatar() {
      return import.meta.env.VITE_APP_URL + "/" + this.userData.avatar || "";
    },
  },
  actions: {
    // 更新系统屏幕宽度
    updateScreenParams(width, heigth) {
      this.systemParams.screenWidth = width;
      this.systemParams.screenHeight = heigth;
    },
    //更新用户信息
    updateUserData(value) {
      Object.assign(this.userData, value);
    },
    //更新用户昵称
    updatePermission(premission) {
      this.premission = premission;
    },
    //更新用户登录状态
    updateIsLogin(value) {
      this.isLogin = value;
    },
    //更新头像
    updateAvatar(value) {
      this.userData.avatar = value;
    },
    //退出登录清除用户信息
    clearUserData() {
      this.isLogin = false;
      this.userData = {
        id: null,
        username: null,
        nickname: null,
        school: null,
        college: null,
        major: null,
        number: null,
        realName: null,
        gender: null,
        github: null,
        blog: null,
        email: null,
        avatar: null,
        signature: null,
        authLevel: null,
      };
    },
  },
});

export default useOjStore;
